package com.cssy.xxkj.basic.web;

import java.io.File;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.cssy.xxkj.basic.util.FileUtil;
@Controller
@RequestMapping("/util")
public class UtilController {
	public static final String UPLOADFILE="/public/UploadFile";
	@RequestMapping("/toUploadFile")
	public String toUploadFile(HttpServletRequest request,
			HttpServletResponse response){
		return UPLOADFILE;
	}
	@RequestMapping("/fileUplod")
	public void fileUpload(HttpServletRequest request,
			HttpServletResponse response){
		 MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		 MultipartFile  file=multipartRequest.getFile("file");
		FileUtil util=new FileUtil();
		PrintWriter out=null;
		String path="E:/fileupload";
		try {
			out=response.getWriter();
			String fileName=file.getOriginalFilename();
			if(util.checkSuffix(fileName)){
				File tfile=new File(path);
				if(!tfile.exists()){
					tfile.mkdirs();
				}
				path=path+File.separator+UUID.randomUUID().toString()+"."+
						util.getSuffix(fileName);
				File trFile=new File(path);
				util.createFile(file, trFile); 
				out.print("0,"+path+","+path);
			}else{
				out.print("1");
			}
		} catch (Exception e) {
			e.printStackTrace();
			out.print("2");
		}
	}
	
}
